package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TourismPact;
import com.ruoyi.system.domain.TourismRooms;
import com.ruoyi.system.domain.TourismUser;
import com.ruoyi.system.mapper.TourismPactMapper;
import com.ruoyi.system.mapper.TourismRoomsMapper;
import com.ruoyi.system.mapper.TourismUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TourismAccommodationMapper;
import com.ruoyi.system.domain.TourismAccommodation;
import com.ruoyi.system.service.ITourismAccommodationService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 民宿信息Service业务层处理
 *
 * @author gqd
 * @date 2020-12-25
 */
@Service
public class TourismAccommodationServiceImpl implements ITourismAccommodationService
{
    @Autowired
    private TourismAccommodationMapper tourismAccommodationMapper;
    @Resource
    private TourismRoomsMapper tourismRoomsMapper;
    @Resource
    private TourismPactMapper tourismPactMapper;
    @Resource
    private TourismUserMapper tourismUserMapper;

    /**
     * 查询民宿信息
     *
     * @param id 民宿信息ID
     * @return 民宿信息
     */
    @Override
    public TourismAccommodation selectTourismAccommodationById(Long id)
    {
        return tourismAccommodationMapper.selectTourismAccommodationById(id);
    }

    /**
     * 查询民宿信息列表
     *
     * @param tourismAccommodation 民宿信息
     * @return 民宿信息
     */
    @Override
    public List<TourismAccommodation> selectTourismAccommodationList(TourismAccommodation tourismAccommodation)
    {
        return tourismAccommodationMapper.selectTourismAccommodationList(tourismAccommodation);
    }

    /**
     * 新增民宿信息
     *
     * @param tourismAccommodation 民宿信息
     * @return 结果
     */
    @Override
    public int insertTourismAccommodation(TourismAccommodation tourismAccommodation)
    {
        tourismAccommodation.setCreateTime(DateUtils.getNowDate());
        return tourismAccommodationMapper.insertTourismAccommodation(tourismAccommodation);
    }

    /**
     * 修改民宿信息
     *
     * @param tourismAccommodation 民宿信息
     * @return 结果
     */
    @Override
    public int updateTourismAccommodation(TourismAccommodation tourismAccommodation)
    {
        return tourismAccommodationMapper.updateTourismAccommodation(tourismAccommodation);
    }

    /**
     * 批量删除民宿信息
     *
     * @param ids 需要删除的民宿信息ID
     * @return 结果
     */
    @Override
    public int deleteTourismAccommodationByIds(Long[] ids)
    {
        return tourismAccommodationMapper.deleteTourismAccommodationByIds(ids);
    }

    /**
     * 删除民宿信息信息
     *
     * @param id 民宿信息ID
     * @return 结果
     */
    @Override
    public int deleteTourismAccommodationById(Long id)
    {
        return tourismAccommodationMapper.deleteTourismAccommodationById(id);
    }

    /**
     * 查询所有民宿
     * @param hotelName
     * @return
     */
    @Override
    public AjaxResult selectAll(String hotelName) {
        List<TourismAccommodation> tourismAccommodations = tourismAccommodationMapper.selectAll(hotelName);
        if(tourismAccommodations.size() == 0 || tourismAccommodations == null){
            return AjaxResult.error("暂时还没有任何宾馆的信息！");
        }
        return AjaxResult.success(tourismAccommodations);
    }

    /**
     * 根据民宿id查询民宿详细信息
     * @param hotelId
     * @return
     */
    @Override
    public AjaxResult selectByHotelId(Integer hotelId,Integer userId) {
        TourismUser tourismUser = tourismUserMapper.selectByUserId(userId);

        TourismAccommodation tourismAccommodation = tourismAccommodationMapper.selectByHotelId(hotelId);
        if(tourismAccommodation == null){
            return AjaxResult.error("暂时还没有该宾馆的信息！");
        }
        List<TourismRooms> tourismRooms = tourismRoomsMapper.selectRoomsByHotelId(hotelId);
        if(tourismRooms.size() == 0 || tourismRooms == null){
            return  AjaxResult.error("暂时还没有该民宿的房间信息！");
        }
        tourismAccommodation.setTourismRooms(tourismRooms);
        if(tourismUser == null || userId == null){
            tourismAccommodation.setFlag("0");
        }else{
            TourismPact tourismPact = tourismPactMapper.selectSpotPact2(hotelId,userId);
            if(tourismPact == null){
                tourismAccommodation.setFlag("0");
            }else {
                tourismAccommodation.setFlag("1");
            }
        }
        return AjaxResult.success(tourismAccommodation);
    }

    /**
     * 精选民宿的查询
     * @param hotelName
     * @return
     */
    @Override
    public AjaxResult selectStatus(String hotelName) {
        List<TourismAccommodation> tourismAccommodations = tourismAccommodationMapper.selectStatus(hotelName);
        if(tourismAccommodations.size() == 0 || tourismAccommodations == null){
            return AjaxResult.error("暂时还没有精选民宿的信息！");
        }
        return AjaxResult.success(tourismAccommodations);
    }
}
